package com.test.controller;

import com.test.pojo.SystemFunction;
import com.test.pojo.SystemUser;
import com.test.service.SystemFunctionService;
import com.test.service.SystemUserService;
import com.test.vo.SystemFunctionItem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/system/user")
public class SystemUserController {

    @Autowired
    private SystemUserService systemUserService;

    @Autowired
    private SystemFunctionService systemFunctionService;

    @RequestMapping("/login")
    public String login(SystemUser systemUser, HttpSession session, RedirectAttributes redirectAttributes) throws UnsupportedEncodingException {
        SystemUser user = systemUserService.login(systemUser);
        if (user != null) {
            System.out.println("user.getRoleId() = " + user.getRoleId());
            List<SystemFunctionItem> functions = systemFunctionService.getAllSystemFunctionByRoleId(user.getRoleId());
            session.setAttribute("user", user);
            session.setAttribute("functions", functions);
            System.out.println("items.size() = " + functions.size());
            return "main/main";
        }else {
            redirectAttributes.addAttribute("msg", "用户名和密码错误");
            return "redirect:/index.jsp";
        }

    }

    @RequestMapping("/loginOut")
    public String loginOut(HttpSession session){
        session.invalidate();
        return "redirect:/index.jsp";

    }



}
